﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web.UI;

namespace Org.Eurekaa.DNJ.Settings
{
    [Serializable()]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class ClientFunction
    {
        private string _name = null;
        [NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private List<string> _params = new List<string>();
        [NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [Browsable(false)]
        public List<string> Parameters
        {
            get { return _params; }
            set { _params = value; }
        }

        private string _body = "";
        [NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }


        public void SetAnonymous()
        {
            this.Name = null;
        }

        public ClientFunction()
        {
        }

        public ClientFunction(string name)
        {
            this.Name = name;
        }

        public ClientFunction(string name, String[] parameters)
        {
            this.Name = name;
            for (int i = 0; i <= parameters.Length; i++)
                this.Parameters.Add(parameters[i]);
        }
        public ClientFunction(string name, String[] parameters, string body)
        {
            this.Name = name;
            for (int i = 0; i <= parameters.Length; i++)
                this.Parameters.Add(parameters[i]);
            this.Body = body;
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (Name == null) sb.Append("function(");
            else sb.Append("function " + Name + "(");

            for (int i = 0; i < Parameters.Count; i++ )
            {
                if (i > 0) sb.Append(",");
                sb.Append(Parameters[i]);
            }
            sb.Append("){" + Body + "}");

            return sb.ToString();
        }


    }
}
